    //
//  ModifyPasswordController.m
//  MClient
//
//  Created by Wang Fei on 10-8-6.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ModifyPasswordController.h"
#import "MServerDAO.h"

@interface ModifyPasswordController(Private)
//UITextField *oldPWord;
//UITextField *newPWord;
//UITextField *newConfirmPWord;
@end

@implementation ModifyPasswordController

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	[super viewDidLoad];
	
	self.title = @"修改密码";
	
	self.tableview.backgroundColor = [UIColor clearColor];
}

- (void)dealloc {
	[oldPWord release];
	[newPWord release];
	[newConfirmPWord release];
    [super dealloc];
}

#pragma mark ---- UITableView方法 ----

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell;
	if (indexPath.row < 3) {
		static NSString *cellIdentifier_Style1 = @"cellIdentifier_Style1";
		cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier_Style1];
		if (cell == nil) {
			cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier_Style1] autorelease];
			cell.accessoryType = UITableViewCellAccessoryNone;
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
			cell.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
			cell.textLabel.backgroundColor = [UIColor clearColor];
			
			UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(110, 8, 180, 27)];
			[field addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
			field.borderStyle = UITextBorderStyleNone;
			field.clearsOnBeginEditing = NO;
			field.returnKeyType = UIReturnKeyDone;
			field.textColor = [UIColor colorWithRed:0.282 green:0.341 blue:0.455 alpha:1];
			field.backgroundColor = [UIColor clearColor];
			field.autoresizingMask = nTableCellAutoresizingMask;
			[cell addSubview:field];
			field.tag = indexPath.row+100;
			[field release];
		}
		UITextField *field = (UITextField *)[cell viewWithTag:indexPath.row+100];
		if (indexPath.row == 0) {
			cell.textLabel.text = @"原密码";
			field.placeholder = @"输入原密码";
			field.secureTextEntry = YES;
			oldPWord = [field retain];
		} else if (indexPath.row == 1) {
			cell.textLabel.text = @"新密码";
			field.placeholder = @"输入新密码";
			field.secureTextEntry = YES;
			newPWord = [field retain];
		} else {
			cell.textLabel.text = @"确认密码";
			field.placeholder = @"确认密码";
			field.secureTextEntry = YES;
			newConfirmPWord = [field retain];
		}
	} else {
		static NSString *cellIdentifier_Style2 = @"cellIdentifier_Style2";
		cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier_Style2];
		if (cell == nil) {
			cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier_Style2] autorelease];
			cell.accessoryType = UITableViewCellAccessoryNone;
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
			cell.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
			
			UIButton* applyBtn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
			applyBtn.frame = CGRectMake(35, 7, 80, 30);
			applyBtn.autoresizingMask = nTableCellAutoresizingMask;
			[applyBtn setTitle:@"修改" forState:UIControlStateNormal];
			[applyBtn addTarget:self action:@selector(sendApply) forControlEvents:UIControlEventTouchUpInside];
			[cell.contentView addSubview:applyBtn];
			[applyBtn release];
			
			UIButton* resetBtn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
			resetBtn.frame = CGRectMake(200, 7, 80, 30);
			resetBtn.autoresizingMask = nTableCellAutoresizingMask;
			[resetBtn setTitle:@"重置" forState:UIControlStateNormal];
			[resetBtn addTarget:self action:@selector(resetInput) forControlEvents:UIControlEventTouchUpInside];
			[cell.contentView addSubview:resetBtn];
			[resetBtn release];
		}
	}
	
	return cell;
}

// 向服务器发送修改请求
- (void)sendApply
{
	@try {
		if ([newPWord.text isEqualToString:newConfirmPWord.text]) {
			[[MServerDAO sharedInstance] modifyPasswordAsync:oldPWord.text 
													password:newPWord.text 
													delegate:self];
		}
		else {
			[self messageBox:@"两次输入密码不一致" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定"];
		}
	}
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}
			 
- (void)getDataFromServerSuccess: (NSMutableDictionary *)returnValue{
	[self messageBox:@"密码修改成功！" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定"];
}
			 
			 
// 将输入框内容全部清空
- (void)resetInput
{
	oldPWord.text = @"";
	newPWord.text = @"";
	newConfirmPWord.text = @"";
}

#pragma mark --- UIAlertViewDelegate --
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	[self.navigationController popViewControllerAnimated:YES];
}

- (IBAction) textFieldDone:(id)sender
{
	[sender resignFirstResponder];
}

@end
